package com.hjs.tools;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author violetfreesia
 * @date 2020/5/21 21:56
 */
@Slf4j
public class Mapper<T> {
    final SqlSession sqlSession;
    final T mapper;

    public Mapper(Class<T> mapperClass) {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        sqlSession = sqlSessionFactory.openSession(true);
        mapper = sqlSession.getMapper(mapperClass);
    }

    public T getMapper() {
        return mapper;
    }

    public void closeSession() {
        sqlSession.close();
    }

    /**
     * 根据result决定事务回滚还是提交
     *
     * @param result sql执行结果
     */
    public void SessionEnd(boolean result) {
        if (result) {
            sqlSession.commit();
        } else {
            sqlSession.rollback();
        }
    }

    /**
     * 根据result决定事务回滚还是提交,在回滚后输出错误日志信息
     *
     * @param result   sql执行结果
     * @param errorMsg 需要输出的错误日志
     */
    public void SessionEnd(boolean result, String errorMsg) {
        if (result) {
            sqlSession.commit();
        } else {
            sqlSession.rollback();
            log.error(errorMsg);
        }
    }

    private static SqlSessionFactory getSqlSessionFactory() {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            log.error("未找到配置文件:" + resource);
        }
        if (inputStream == null)
            throw new NullPointerException("由未找到文件引起的空指针异常");
        return new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Deprecated(forRemoval = true)
    public static <T> T getMapper(Class<T> mapperClass) {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        return sqlSession.getMapper(mapperClass);
    }

    public static SqlSession getSqlSession() {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        return sqlSessionFactory.openSession();
    }

    public static SqlSession getSqlSession(boolean commit) {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        return sqlSessionFactory.openSession(commit);
    }

    public static <T> String toJsonString(T obj) {
        ObjectMapper objectMapper = new ObjectMapper();
        String result = null;
        try {
            result = objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            log.error("序列化失败");
        }
        if (result == null) {
            throw new NullPointerException("由于序列化失败引起的空指针异常");
        }
        return result;
    }

    /**
     * 根据result决定事务回滚还是提交
     *
     * @param result     sql执行结果
     * @param sqlSession sqlSession
     */
    public static void SessionEnd(boolean result, SqlSession sqlSession) {
        if (result) {
            sqlSession.commit();
        } else {
            sqlSession.rollback();
        }
    }

    /**
     * 根据result决定事务回滚还是提交,在回滚后输出错误日志信息
     *
     * @param result     sql执行结果
     * @param sqlSession sqlSession
     * @param errorMsg   需要输出的错误日志
     */
    public static void SessionEnd(boolean result, SqlSession sqlSession, String errorMsg) {
        if (result) {
            sqlSession.commit();
        } else {
            sqlSession.rollback();
            log.error(errorMsg);
        }
    }
}
